﻿using BLL;
using BONGDA.Excel;
using DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BONGDA
{
    public partial class frmMatch : Form
    {
        public frmMatch()
        {
            InitializeComponent();
        }
        int soDoi = 0;
        int soVong = 0;
        private void frmMatch_Load(object sender, EventArgs e)
        {
            // Load danh sach doi bong
            DataTable dtDoiBong = DoiBong_BLL.SelectAll();
            // Load combobox vong dau
            soDoi = dtDoiBong.Rows.Count;
            soVong = ((soDoi / 2 + soDoi % 2) * 2 - 1) * 2;
            for (int i = 0; i < soVong; i++)
            {
                cboVong.Items.Add(i + 1);
            }
            cboVong.SelectedItem = 1;
            lblVong.Text = "--Tổng cộng " + soVong + " vòng--";

            HienThiDoiBong();
            HienThiTranDau();
        }

        private void HienThiTranDau()
        {
            DataTable table;
            DataColumn column;
            DataRow row;
            // Load danh sach Tran dau
            DataTable dtTranDau = LichThiDau_BLL.SelectbyVong(Convert.ToInt32(cboVong.SelectedItem));

            table = new DataTable("TranDau");
            column = new DataColumn();

            // column Ngay
            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Ngay";
            column.AutoIncrement = false;
            //column.Caption = "Đội bóng";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // column Gio
            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Gio";
            column.AutoIncrement = false;
            //column.Caption = "Đội bóng";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // column logo1
            column = new DataColumn();
            column.DataType = Type.GetType("System.Byte[]");
            column.ColumnName = "Logo1";
            column.AutoIncrement = false;
            //column.Caption = "Logo";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Doi1";
            column.AutoIncrement = false;
            //column.Caption = "Đội bóng";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "BanThang";
            column.AutoIncrement = false;
            //column.Caption = "Điểm";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "BanThua";
            column.AutoIncrement = false;
            //column.Caption = "Điểm";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Doi2";
            column.AutoIncrement = false;
            //column.Caption = "Đội bóng";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // column logo1
            column = new DataColumn();
            column.DataType = Type.GetType("System.Byte[]");
            column.ColumnName = "Logo2";
            column.AutoIncrement = false;
            //column.Caption = "Logo";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            for (int i = 0; i < dtTranDau.Rows.Count; i++)
            {
                row = table.NewRow();
                DateTime time = Convert.ToDateTime(dtTranDau.Rows[i]["Ngay"]);
                row["Ngay"] = "" + time.Day + "/" + time.Month + "/" + time.Year + "";
                time = Convert.ToDateTime(dtTranDau.Rows[i]["Gio"]);
                row["Gio"] = "" + time.Hour + ":" + time.Minute + "";
                if (true)
                {
                    row["BanThang"] = "-";
                    row["BanThua"] = "-";
                }
                else
                {
                    row["BanThang"] = dtTranDau.Rows[i]["BanThang"];
                    row["BanThua"] = dtTranDau.Rows[i]["BanThua"];
                }
                DoiBong_DTO dtoDoiBongTam = DoiBong_BLL.SelectByID(Convert.ToInt32(dtTranDau.Rows[i]["Doi1"]));
                row["Logo1"] = dtoDoiBongTam.ByteLogo;
                row["Doi1"] = dtoDoiBongTam.TenDoiBong;
                dtoDoiBongTam = DoiBong_BLL.SelectByID(Convert.ToInt32(dtTranDau.Rows[i]["Doi2"]));
                row["Doi2"] = dtoDoiBongTam.TenDoiBong;
                row["Logo2"] = dtoDoiBongTam.ByteLogo;
                table.Rows.Add(row);
            }
            dgvTranDau.DataSource = table; // Xong table  tran dau
        }
        private void HienThiDoiBong()
        {
            DataTable dtDoiBong = DoiBong_BLL.SelectAll();
            DataTable table;
            DataColumn column;
            DataRow row;

            // Load danh sach doi bong
            table = new DataTable("DoiBong");
            column = new DataColumn();

            column.DataType = Type.GetType("System.Byte[]");
            column.ColumnName = "Logo";
            column.AutoIncrement = false;
            //column.Caption = "Logo";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.Int32");
            column.ColumnName = "Diem";
            column.AutoIncrement = false;
            //column.Caption = "Điểm";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "TenDoiBong";
            column.AutoIncrement = false;
            //column.Caption = "Đội bóng";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);
            for (int i = 0; i < dtDoiBong.Rows.Count; i++)
            {
                row = table.NewRow();
                row["Logo"] = dtDoiBong.Rows[i]["Logo"];
                row["Diem"] = dtDoiBong.Rows[i]["Diem"];
                row["TenDoiBong"] = dtDoiBong.Rows[i]["TenDoiBong"];
                table.Rows.Add(row);
            }
            dgvDoiBong.DataSource = table; // Xong table  doi bong
        }
        private void btnThucHien_Click(object sender, EventArgs e)
        {
            frmLapLichThiDau frm = new frmLapLichThiDau();
            frm.ShowDialog();
            HienThiTranDau();
        }

        private void btnL_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(cboVong.SelectedItem) != 1)
            {
                cboVong.SelectedItem = Convert.ToInt32(cboVong.SelectedItem) - 1;
                HienThiTranDau();
            }
        }

        private void btnR_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(cboVong.SelectedItem) != soVong)
            {
                cboVong.SelectedItem = Convert.ToInt32(cboVong.SelectedItem) + 1;
                HienThiTranDau();
            }
        }

        private void cboVong_SelectedIndexChanged(object sender, EventArgs e)
        {
            HienThiTranDau();
        }

        private void btnEx_Click(object sender, EventArgs e)
        {
            try
            {
                string oldPath = Directory.GetCurrentDirectory();
                SaveFileDialog sf = new SaveFileDialog();
                sf.ShowDialog();
                string strFileName = sf.FileName;
                Directory.SetCurrentDirectory(oldPath);
                ExcelWrite myExcel = new ExcelWrite();
                dataGridView1.DataSource = LichThiDau_BLL.SelectAll();
                List<String> list = myExcel.DGVToExcel(dataGridView1);
                String filePath = Directory.GetCurrentDirectory() + strFileName;
                myExcel.WriteDateToExcel(strFileName, list, "A1", "F1", "A2");
                MessageBox.Show("Export dữ liệu thành công!", "Thông báo",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                Process.Start(strFileName);
            }
            catch (Exception)
            {
            }
        }
    }
}
